package com.wdb007.venus.wss.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wdb007.venus.svc.facade.ProcessInstructionService;
import com.wdb007.venus.svc.facade.process.model.ProcessInstructionLogDTO;
import com.wdb007.venus.wss.channelgroup.BookCaseChannelGroups;
import com.wdb007.venus.wss.constants.PushActionEnum;
import com.wdb007.venus.wss.service.WebSocketServerService;

import io.netty.channel.ChannelHandlerContext;

@Service("bookCaseUnlockResService")
public class BookCaseUnlockResService implements WebSocketServerService {

	private static Logger logger = LoggerFactory.getLogger(BookCaseUnlockResService.class);
	
//	@Autowired
//	private BookgridService bookgridService;
	@Autowired
	private ProcessInstructionService processInstructionService;	
	@Override
	public String doService(ChannelHandlerContext ctx, String request) throws Exception {
		JSONObject jsonObj = JSON.parseObject(request);
		logger.info("BookCaseUnlockRes 接收websocket响应信息: {}", jsonObj.toJSONString());
		String bookCaseId = BookCaseChannelGroups.getBookCaseId(ctx.channel());
		//1.首先根据pushId来判断是否已经处理，如果已经处理，则不做任何处理。
		String pushId = jsonObj.getString("pushId");
		ProcessInstructionLogDTO pi = new ProcessInstructionLogDTO();
		pi.setContent(jsonObj.toJSONString());
		pi.setBookgridCode(bookCaseId);
		pi.setInstructionGroupId(Long.valueOf(pushId));
		processInstructionService.bookCaseUnlockRes(pi);
		jsonObj.remove("statuses");
		jsonObj.put("action", PushActionEnum.bookcase_status_req.name());
		
		logger.info("debug {} : 书柜[{}],响应内容[{}]", jsonObj.getString("action"), bookCaseId, jsonObj.toJSONString());
		return null;
	}

}
